package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/model"
	"server/pkg/app"
)

func CreateTestCase(c *gin.Context) {
	testcase := &model.TestCase{}
	_ = c.ShouldBind(testcase)
	if len(testcase.Name) == 0 {
		c.String(http.StatusOK, "用例名不能为空")
		return
	}
	//if testcase.CaseID == 0 {
	//	c.String(http.StatusOK, "用例ID不为空")
	//	return
	//}
	//if testcase.CaseMeauMD5 != "" {
	//	testcase.CaseMeauMD5 = testcase.CaseMeauMD5
	//}

	if err := testcase.Create(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "用例创建成功")
}

func TestCaseList(c *gin.Context) {
	pages := &app.Page{}
	pages.Info(c)

	testcase := &model.TestCase{}
	id := c.Query("id")
	if len(id) == 0 {
		testcase_list, total := testcase.List(pages)
		c.JSON(http.StatusOK, gin.H{
			"total": total,
			"data":  testcase_list,
		})
		return
	} else {
		testcase_list, total := testcase.QueryById(id, pages)
		c.JSON(http.StatusOK, gin.H{
			"total": total,
			"data":  testcase_list,
		})
		return
	}

}

func TestCaseInfo(c *gin.Context) {}

func UpdateTestCase(c *gin.Context) {
	meau := &model.TestCase{}
	_ = c.ShouldBind(meau)
	if err := meau.Update(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "用例修改成功")
}

func DeleteTestCase(c *gin.Context) {
	meau := &model.TestCase{}
	meau.Name = c.Param("name")
	if err := meau.Delete(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "用例删除成功")
}
